#!/usr/bin/env sh

# 主机目录 => 容器目录
# DB_DATA => /var/lib/mysql
# DB_PATH/log => /var/log/mysql
# DB_PATH => /data

# 加载配置
DB_DATA_PATH=/var/lib/mysql
DB_LOG_PATH=/var/log/mysql
. /data/install.conf

# 选择备份的数据库
database=${DB_DATABASE}
if [ ! -z $1 ]; then
  database=$1
fi

# 创建目录
mkdir -p /data/backup/${database}
chmod -R 777 /data/backup

# 备份路径
path=/data/backup/${database}/${database}_$(date +"%Y%m%d%H%M%S").sql.gz
echo "$(date +"%Y-%m-%d %H:%M:%S") backup to: ${path}" >> ${DB_LOG_PATH}/${database}.log

mysqldump -u${DB_USERNAME} -p${DB_PASSWORD} ${database} | gzip > ${path}